package demo1;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class DEMO_01 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你想要查找的目录路径");
        String path = scanner.nextLine();
        File file = new File(path);
        if (!file.exists() || !file.isDirectory()) {
            System.out.println("没有查到你想要查找的目录");
            return;
        }
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                System.out.println("目录： " + files[i].getCanonicalFile());
            } else {
                System.out.println("文件： " + files[i].getCanonicalFile());
            }
        }
        System.out.println("请输入你要查找的文件名称");
        String name = scanner.nextLine();
        if (name.isEmpty() || name.isBlank()) {
            System.out.println("你输入的文件名无效");
            return;
        }
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile() && files[i].getName().equals(name)) {
                System.out.println("已经查到你想要查找的文件你想要删除吗？Yes：1  No：2");
                int isDelete = scanner.nextInt();
                if (isDelete == 1) {
                    files[i].delete();
                } else {
                    return;
                }

            }
        }
        System.out.println("没有查到你想要的文件");
        scanner.close();


    }

    public File findFile(String name, File file) {
        if (file.isFile()) {
            if (file.getName().equals(name)) {
                return file;
            } else {
                return null;
            }
        } else if (file.isDirectory() && file.listFiles().length == 0) {
            return null;
        } else if (file.isDirectory() && file.listFiles().length != 0) {
            File[] files = file.listFiles();

            for (int i = 0; i < files.length; i++) {
                File file1 = findFile(name, files[i]);
                if (file1 != null) {
                    return file1;
                }
            }
        }
        return null;
    }
}
